﻿using DFGK.IService.BBS;
using DFGK.Service;
using DFGK.Service.BBS;
using DFGK.Service.LY;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 装备报废报损
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentBadController : BaseController
    {
        private readonly IEquipmentBadService _equiBadService;
        public EquipmentBadController(IWebHostEnvironment environment, IEquipmentBadService equiBadService) : base(environment)
        {
            _equiBadService = equiBadService;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="title"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? materialName, int pageIndex = 1, int pageSize = 50)
        {

            int total = 0;
            var page = _equiBadService.GetPageData(materialName, pageIndex, pageSize, out total);
            return Ok(BaseResult.Result(page, "查询成功！", "200", total));
        }

        /// <summary>
        /// 删除装备报损信息
        /// </summary>
        /// <param name="equipmentBadId">资讯id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelEquipmentBad(long equipmentBadId)
        {
            var result = _equiBadService.DelEquimentBad(equipmentBadId, out string msg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 新增/修改装备报损
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitEquipmentBad(EquipmentBad model)
        {
            if (model.Id > 0)
            {
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateUserName = base.BaseGetNowUserName();
            }
            bool result = _equiBadService.SubmitEquipmentBad(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// Id查询
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEquipmentBad(long equipmentBadId)
        {
            var cobj = _equiBadService.GetEquipmentBad(equipmentBadId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 装备报废统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public IActionResult EquipmentBadTongJi()
        {
            return Ok(BaseResult.Result(_equiBadService.EquipmentBadTongJi(), "查询成功！", "200", 0));

        }
        /// <summary>
        /// 装备报废审批
        /// </summary>
        /// <param name="equipmentBadId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EquipmentBadSP(long equipmentBadId, string? status)
        {
            string errMsg = "";
            bool result = _equiBadService.EquipmentBadSP(equipmentBadId, status, out errMsg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "审批成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "审批失败！" + errMsg, "500"));
        }

        /// <summary>
        /// 保修工单信息管理导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EquipmentBadDown()
        {
            var list = _equiBadService.GetEquipmentBadDown();
            if (list != null)
                return EquipmentBadDown(list);
            else
                return Ok();
        }

        /// <summary>
        /// 报销工单数据统计导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EquipmentBadMsgDown()
        {
            var list = _equiBadService.GetEquipmentBadDown();
            if (list != null)
                return EquipmentBadMsgDown(list);
            else
                return Ok();
        }
    }
}
